package ljl.alg.wangzheng_camp.round1.twopointers;

/**
* 肯定是从左往右好吧！也不一定，我试试？
* */
public class _283_move_0 {
    
    public void moveZeroes(int[] nums) {
        int zero = 0, left = 0;
        while (left < nums.length) {
            if (nums[left] != 0) {
                swap(nums, left, zero++);
            }
            left++;
        }
        
    }
    /*
    * [0,1,0,3,12]
    * 0 1 12 3 0
    * 3 1 12 0 0
    * */
    /**
     * 倒着拷贝也是错的，只能正着拷贝
     * */
    class wrong_again {
        public void moveZeroes(int[] nums) {
            int right = nums.length - 1, zero = right;
            while (right >= 0) {
                if (nums[right] == 0) {
                    swap(nums, right, zero++);
                }
                right--;
            }
        }
    }
    
    /*
    * 错在哪？
    * 清楚写了不要改变原有顺序，但是你没当回事！
    * */
    class wrong {
        public void moveZeroes(int[] nums) {
        
            int left = 0, right = nums.length - 1;
            while (left < right) {
                while (left < right && nums[left] != 0) left++;
                while (left < right && nums[right] == 0) right--;
                swap(nums, left++, right--);
            }
        }
    }
    
    class ans {
        public void moveZeroes(int[] nums) {
            int left = 0, right = 0, n = nums.length;
            while (right < n) {
                if (nums[right] != 0) {
                    swap(nums, left, right);
                    left++;
                }
                right++;
            }
        }
    }
    private void swap(int[] nums, int left, int right) {
        if (left != right) {
            int t = nums[left];
            nums[left] = nums[right];
            nums[right] = t;
        }
    }
}
